package base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;

import com.aimer.common.R;
import com.blankj.utilcode.util.LogUtils;


public abstract class QFragment<V extends ViewDataBinding> extends Fragment implements  QUIOperation  {

    protected final String TAG = getClass().getSimpleName();
    protected V binding;

    private  boolean mStateEnable ;

//

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {


        binding = DataBindingUtil.inflate(inflater, getLayoutRes(), container, false);
        // 使用DataBinding来渲染布局
//        _binding = DataBindingUtil.inflate(inflater, R.layout.books_frgment, container, false)
//        viewModel = ViewModelProvider(this).get(BookHomeViewModel::class.java)

        initView();
        initData();
        initListener();
        networkMessage();

        return binding.getRoot();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtils.dTag(TAG, "onCreate");
    }


    @Override
    public void onStart() {
        super.onStart();
        LogUtils.dTag(TAG, "onStart");

        mStateEnable = true;
    }


    @Override
    public void onResume() {
        super.onResume();
        LogUtils.dTag(TAG, "onResume");
        mStateEnable = true;
    }


    @Override
    public void onSaveInstanceState(Bundle outState) {
        // super.onSaveInstanceState();中将mStateSaved置为true
        mStateEnable = false;
        super.onSaveInstanceState(outState);
    }


    @Override
    public void onPause() {
        super.onPause();
        LogUtils.dTag(TAG, "onPause");
    }

    @Override
    public void onStop() {
        super.onStop();

        mStateEnable = false;
        LogUtils.dTag(TAG, "onStop");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtils.dTag(TAG, "onDestroy");

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (binding != null) {
            binding.unbind();
        }
    }



    /**
     *
     * activity状态是否处于可修改周期内，避免状态丢失的错误
     * @return
     */
    public boolean isStateEnable() {
        return mStateEnable;
    }

    private void initViewDataBinding() {
        binding.setLifecycleOwner(this);
    }

}
